package com.zjlp.im.netty.handler;

import com.zjlp.im.netty.manager.SessionManager;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.socket.nio.NioSocketChannel;

/**
 * @author LinQiang on 2016/12/23.
 * @reviewer
 */
public class DefaultExceptionHandler implements ChannelHandler {
    /**
     * Gets called after the {@link io.netty.channel.ChannelHandler} was added to the actual context and it's ready to handle events.
     *
     * @param ctx
     */
    @Override
    public void handlerAdded(ChannelHandlerContext ctx) throws Exception {

    }

    /**
     * Gets called after the {@link io.netty.channel.ChannelHandler} was removed from the actual context and it doesn't handle events
     * anymore.
     *
     * @param ctx
     */
    @Override
    public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
        System.out.println("被移除的Handler当前上下文名称" + ctx.name());
    }

    /**
     * Gets called if a {@link Throwable} was thrown.
     *
     * @param ctx
     * @param cause
     * @deprecated is part of {@link ChannelHandlerContext}
     */
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        SessionManager sessionManager = SessionManager.getInstance();
        //移除用户原有的通道
        sessionManager.removeUserSessionByCid(ctx.channel().id().toString());
        System.out.println(cause.getCause());
    }
}
